//
//  PicCollectionView.swift
//  Stauts
//
//  Created by Kylin on 16/3/20.
//  Copyright © 2016年 Kylin. All rights reserved.
//

import UIKit
import SDWebImage
// MARK:- 自定义-PicCollectionView
class PicCollectionView: UICollectionView {
   // MARK:- 自定义属性
    var picUrls : [NSURL] = [NSURL]() {
        didSet {
            // 9.设置picCollectionView宽度和高度约束
            let (width,height) = calculateColletionCellSize(picUrls.count)
            picColletionViewWidthCons.constant = width
            picColletionViewHeightCons.constant = height
            
            reloadData()
        }
    }
    
    // MARK:- 约束对象
    @IBOutlet weak var picColletionViewWidthCons: NSLayoutConstraint!
    @IBOutlet weak var picColletionViewHeightCons: NSLayoutConstraint!
   
    
    // MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        
        // 1:设置数据源,代理方法
        dataSource = self
        delegate = self
    
    }
    

}

extension PicCollectionView{
    
    // MARK:- 设算colletionCell的高度
    private func calculateColletionCellSize(count : Int)-> (CGFloat,CGFloat){
        
        // 1:没有图片
        if count == 0 {
            return(0,0)
        }
        
         // 1.1.取出布局
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        // 2:处理一张图片的
        if count == 1 {
            // 取出urlString
            let urlString = picUrls.last!.absoluteString
            // 2.2.取出对应的图片
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(urlString)
            // 2.3.设置流水布局
            let width = image.size.width * 2
            let height = image.size.height * 2
            layout.itemSize = CGSizeMake(width, height)
            layout.minimumInteritemSpacing = 0
            layout.minimumLineSpacing = 0
            
            return(width,height)
        }
        
        // 3.1:设置布局
        let imageWH = (UIScreen.mainScreen().bounds.width - 2 * edageMargin - 2 * padding) / 3
        layout.itemSize = CGSize(width: imageWH, height: imageWH)
        layout.minimumInteritemSpacing = padding
        layout.minimumLineSpacing = padding
        // 3.2:四张图片
        if count == 4 {
            let width = 2 * imageWH + padding
            let height = 2 * imageWH + padding
            
            return(width,height)
        }
        // 4.其他张图片
        // 4.1.计算行数
        let row = CGFloat((count - 1) / 3 + 1)
        // 4.2.计算宽度和高度
        let width = imageWH * 3 + padding * 2
        let height = row * imageWH + (row - 1) * padding
        
        return(width,height)
    }
    

}



// MARK:- PicCollectionDataSource
extension PicCollectionView : UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        // 1:创建cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PicViceCell", forIndexPath: indexPath) as! PicCollectionViewCell
        // 2:给cell设置数据
        cell.picUrl = picUrls[indexPath.item]
        
        return cell
    }
}
// MARK:- CollectionView的代理方法
extension PicCollectionView : UICollectionViewDelegate {
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // 1.获取通知发送时携带的信息
        let userInfo = [showPhotoBrowserVcNoteIndex :indexPath,showPhotoBrowserVcNotePicURLs :picUrls]
        
        // 2.发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(showPhotoBrowserVcNote, object: self, userInfo: userInfo)
        
    }
}
// MARK:- 自定义-PicCollectionViewCell
class PicCollectionViewCell : UICollectionViewCell {
    // MARK:- 定义属性
    var picUrl : NSURL? {
        didSet {
            picImageView.sd_setImageWithURL(picUrl, placeholderImage: UIImage(named: "empty_picture"))
        }
    }
    // MARK:- 控件属性
    @IBOutlet weak var picImageView: UIImageView!
}
// MARK:- photoBroserPresentedDelegate
extension PicCollectionView : PhotoBrowserPresentedDelegate{
    func startRectForPresentedView(indexPath: NSIndexPath) -> CGRect {
        
        // 1:取出cell
        let cell = self.cellForItemAtIndexPath(indexPath)!
        
        // 2.计算cell相当于window的位置
        let startRect = self.convertRect(cell.frame, toCoordinateSpace:UIApplication.sharedApplication().keyWindow!)
        
        return startRect
    }
    
    func endRectForPresentedView(indexPath: NSIndexPath) -> CGRect {
        // 1.取出indexPath对应的图片
        let picURl = picUrls[indexPath.item]
        let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(picURl.absoluteString)
         // 2.计算图片放大后的位置
        let imageW = UIScreen.mainScreen().bounds.width
        let imageH = imageW / image.size.width * image.size.height
        let imageX : CGFloat = 0
        var imageY : CGFloat = (UIScreen.mainScreen().bounds.height - imageH) * 0.5
        
        imageY = imageY < 0 ? 0 : imageY
        
        return CGRect(x: imageX, y: imageY, width: imageW, height: imageH)
    }
    
    func imageViewForPresentedView(indexPath: NSIndexPath) -> UIImageView {
         // 1.创建UIImageView的对象
        let imageView = UIImageView()
        
        // 2.设置图片
        let picURL = picUrls[indexPath.item]
        let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(picURL.absoluteString)
        
        imageView.image = image
        
        // 3.设置imageView的属性
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
    }
    
    
}


